# -*- coding:utf-8 -*-
import os
import sys
import nuke
# import nukescripts
if sys.version>'3':
    from PySide2.QtGui import *
    from PySide2.QtCore import *
    from PySide2.QtWidgets import *
else:
    from PySide2.QtGui import *
    from PySide2.QtCore import *
    from PySide2.QtWidgets import *

class ReplaceDirver(QWidget):

    def __init__(self):
        super(ReplaceDirver, self).__init__()

        self.main_layout = QVBoxLayout()
        self.main_layout.setSpacing(15)
        self.setWindowTitle(u'替换路径')
        self.from_layout = QHBoxLayout()
        self.from_label = QLabel(u'开头路径')
        self.from_edit = QLineEdit()
        self.from_layout.addWidget(self.from_label)
        self.from_layout.addWidget(self.from_edit)

        self.to_layout = QHBoxLayout()
        self.to_label = QLabel(u'替换为 :')
        self.to_edit = QLineEdit()
        self.to_layout.addWidget(self.to_label)
        self.to_layout.addWidget(self.to_edit)

        self.btn_layout = QHBoxLayout()
        self.OK_btn = QPushButton('OK')
        self.OK_btn.clicked.connect(self._replace_dirve)
        self.NO_btn = QPushButton('NO')
        self.btn_layout.addWidget(self.OK_btn)
        self.btn_layout.addWidget(self.NO_btn)

        self.main_layout.addLayout(self.from_layout)
        self.main_layout.addLayout(self.to_layout)
        self.main_layout.addLayout(self.btn_layout)
        self.setLayout(self.main_layout)

    def _replace_dirve(self):

        sn = nuke.selectedNodes()
        f = self.from_edit.text()
        t = self.to_edit.text()
        for n in sn:
            try:
                fpath = n['file'].value()
                lastFpath = fpath[len(f):]
                newpath = t + lastFpath
                n['file'].setValue(newpath)

            except:
                pass

if __name__ == '__main__':
    app =QApplication(sys.argv)
    r = ReplaceDirver()
    r.show()
    sys.exit(app.exec_())


